import BaseRequest from "./request";
import {
	Message
} from 'element-ui';
import {
	BASE_URL,
	TIME_OUT
} from "../config";
import LocalCache from "../catch/cache";
import router from "../../router/index";
import store from '../../store/index.js'

const service = new BaseRequest({
	baseURL: BASE_URL,
	timeout: TIME_OUT,
	interceptors: {
		requestInterceptor: (config) => {
			// if (config.url != "/api/rccy/v1/rccy_login") {
			// 	const token = LocalCache.getCache('token') || ""
			// 	config.headers['token'] = token;
			// }
			// return config;
			const rccyUserinfo = localStorage.getItem('rcjj_userinfo')
			if (rccyUserinfo) {
				config.headers["token"] = `${JSON.parse(rccyUserinfo).token}`;
			}
			return config;
			
		},
		requestInterceptorCatch: (error) => {
			return error;
		},
		// responseInterceptor: (res) => {
		// 	if (res.data.code === -1) {
		// 		LocalCache.clearCache()
        //         Message('登录过期，请重新登录');
		// 		router.push('/')
		// 	}
		// 	return res.data
		// },
		responseInterceptor: (res) => {
			if (res.data.code == -1) {
				localStorage.removeItem('rcjj_userinfo')
				store.dispatch('common/init')
				Message.error(res.data.msg)
				setTimeout(() => {
					router.push('/login')
				}, 1000)
			}

			return res.data;
		},
		responseInterceptorCatch: (error) => {
            console.log(error)
			return error
		}
	}
});

export default service;